/**
 * @Description:
 * @param roleName 菜单名称
 * @param status 菜单状态
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/11
 */

var status = "";

/**
 * 初始化
 */
$(function () {
    init();
    tableInit();
});

/**
 * 加载layui样式
 */
function init() {
    layui.use(['form', 'laydate'], function () {
        var form = layui.form;

        //菜单状态选择
        form.on('select(status)', function (data) {
            //  状态值
            if (data.value != null) {
                status = data.value;
            }
        });


    });
}

// 数据展示
function tableInit() {
    layui.use(['form','table','treetable'], function () {
        var form = layui.form
            , table = layui.table
            , treetable = layui.treetable;
        var loading = window.top.layer.load(0, {shade: false});
        // 渲染表格
        treetable.render({
            url : prefixDept + "/list",
            toolbar: '#toolbar',
            treeColIndex: 2,
            treeSpid: 0,
            treeIdName: 'deptId',
            treePidName: 'parentId',
            treeDefaultClose: false,
            elem: '#tb',
            where: {
                //  设置参数
                "status": status,
            },
            cellMinWidth: 200,
            cols: [[
                {checkbox: true},
                {type: 'numbers',title:'序号'},
                {field: 'deptName', title: '部门名称',width: 180},
                {field: 'leader', title: '负责人'},
                {field: 'phone', title: '联系电话'},
                {field: 'email', title: '邮箱'},
                {field: 'orderNum', title: '显示排序'},
                {field: 'status', title: '状态',
                    templet: function (d) {
                        switch (d.status) {
                            case "0" :
                                return "<button class=\"layui-btn layui-btn-sm\">正常</button>";
                            case "1" :
                                return "<button class=\"layui-btn layui-btn-sm layui-btn-warm\">停用</button>";
                            default:
                                return "<button class=\"layui-btn layui-btn-sm layui-btn-danger\">异常</button>";
                        }

                    }
                },
                {field: 'createTime', title: '创建时间'},
                {fixed: 'right', align: 'center',width:170, toolbar: '#tool'}
            ]]
            , done:
                function () {
                    layTool.toplayer.close(loading);
                }
        });

        //头工具栏事件
        table.on('toolbar(tb)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            switch (obj.event) {
                case 'insert':
                    insert();
                    break;
                case 'remove':
                    remove(data);
                    break;
                case 'expand'://全部展开
                    treetable.expandAll('#tb');
                    break;
                case 'fold'://全部折叠
                    treetable.foldAll('#tb');
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(tb)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据

            switch (obj.event) {
                case 'update':
                    //编辑
                    update(data);
                    break;
            }
        });

    });
}


/**
 * @Description:    查询事件
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/7
 */

function serach() {
    tableInit();
}

/**
 * @Description: 打开添加页面
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/19
 */

function insert() {
    var title = "新增部门信息",
        url = prefixOpen + "/web!system!dept!dept_insert",
        width = "30%",
        height = "60%";
    //  打开新窗口
    layTool.openDialog(title,url,width,height,insertOk);
}

/**
 *  保存用户点击事件save
 */
function insertOk(index, layero) {
    // 获取子类窗口的form 表单
    var body = layTool.getChildFrame("body",index);
    var form = $(body[0]).find('#form');
    if(validate_form(form[0])){
        var formObj = form.serializeArray();
        var loading = "";
        // 发送保存地址
        $.ajax({
            url: prefixDept + "/insert",
            data: formObj,
            type:"POST",
            beforeSend :function () {
                loading = layTool.toplayer.load(0, {shade: false});
            },
            success:function (data) {
                layTool.toplayer.close(loading);
                var errcode = data.errcode;
                var errmsg = data.errmsg;
                if(errcode == "0"){
                    tableInit();
                    layTool.closeLayer(index);
                    layTool.showSucAlert(errmsg);
                }else{
                    layTool.showAlert(errmsg);
                }

            },
            error:function (e) {
                layTool.toplayer.close(loading);
                AjaxErro();
            }
        })
    }
}
/**
 * @Description: 部门更新打开页面
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/24
 */

function update(data) {
    var deptId = data.deptId;
    var title = "修改部门信息",
        url = prefixDept + "/openDeptUpdate?deptId=" + deptId,
        width = "30%",
        height = "75%";
    //  打开新窗口
    layTool.openDialog(title, url, width, height, updateOk);
}


/**
 * @Description: 确定更新事件回调
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/7
 */
function updateOk(index, layero) {
    // 获取子类窗口的form 表单
    var body = layTool.getChildFrame("body", index);
    var form = $(body[0]).find('#form');
    if (validate_form(form[0])) {
        var formObj = form.serializeArray();
        // 发送保存地址
        $.ajax({
            url: prefixDept + "/update",
            data: formObj,
            type: "POST",
            beforeSend: function () {
                loading = layTool.toplayer.load(0, {shade: false});
            },
            success: function (data) {
                layTool.toplayer.close(loading);
                var errcode = data.errcode;
                var errmsg = data.errmsg;
                if (errcode == "0") {
                    tableInit();
                    layTool.closeLayer(index);
                    layTool.showSucAlert(errmsg);
                } else {
                    layTool.showAlert(errmsg);
                }

            },
            error: function (e) {
                layTool.toplayer.close(loading);
                AjaxErro();
            }
        })
    }
}


/**
 * @Description:   删除操作
 * @Param:
 * @return:
 * @Author: Mr.Yan
 * @Date: 2018/12/8
 */

function remove(data) {
    if (!isEmpty(data)) {
        var dataLength = data.length;

        if (dataLength > 0) {
            layTool.showConfirm("确定要删除您选择的" + dataLength + "个部门吗？", function () {
                var loading = "";
                var deptIds = "";
                for (var i = 0; i < dataLength; i++) {
                    deptIds += data[i].deptId + ",";
                }
                $.ajax({
                    url: prefixDept + "/deleteDept",
                    data: {
                        deptIds: deptIds.substring(0, deptIds.length - 1)
                    },
                    beforeSend: function () {
                        loading = layTool.toplayer.load(0, {shade: false});
                    },
                    success: function (data) {
                        layTool.toplayer.close(loading);
                        if (!isEmpty(data)) {
                            var errcode = data.errcode;
                            var errmsg = data.errmsg;
                            if (errcode == "0") {
                                tableInit();
                                layTool.showSucAlert(errmsg);
                            } else {
                                layTool.showAlert(errmsg);
                            }
                        }
                    },
                    error: function () {
                        layTool.toplayer.close(loading);
                        AjaxErro();
                    }
                })

            });
        } else {
            layTool.showMsg("只能同时选择1个或多个部门，您选择了" + dataLength + "个");
        }
    } else {
        layTool.showMsg("请选择要删除的部门信息");
    }

}
